package cn.lger.icollege.controller;

import cn.lger.icollege.entity.Admin;
import cn.lger.icollege.entity.Classroom;
import cn.lger.icollege.entity.Student;
import cn.lger.icollege.entity.Teacher;
import cn.lger.icollege.service.AdminService;
import cn.lger.icollege.service.ClassroomService;
import cn.lger.icollege.service.StudentService;
import cn.lger.icollege.service.TeacherService;
import cn.lger.icollege.util.FileUploadUtil;
import cn.lger.icollege.util.SecurityResourceUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Code that Changed the World
 * Pro said
 * Created by Pro on 2018-04-29.
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Value("${file.upload.path}")
    private String prefixPath;

    @Resource
    private AdminService adminService;
    @Resource
    private StudentService studentService;
    @Resource
    private TeacherService teacherService;
    @Resource
    private ClassroomService classroomService;

    @PostMapping("/students/excel")
    @ResponseBody
    public boolean postStudents(MultipartFile file){
        File excelFile;
        try {
            excelFile = FileUploadUtil.uploadToTemp(file, prefixPath, FileUploadUtil.randomUUID());
        } catch (IOException e) {
            throw new RuntimeException("文件上传失败");
        }
        adminService.saveStudentsByExcel(excelFile);
        return excelFile.delete();
    }

    @PostMapping("/teachers/excel")
    @ResponseBody
    public boolean postTeachers(MultipartFile file){
        File excelFile;
        try {
            excelFile = FileUploadUtil.uploadToTemp(file, prefixPath, FileUploadUtil.randomUUID());
        } catch (IOException e) {
            throw new RuntimeException("文件上传失败");
        }
        adminService.saveTeachersByExcel(excelFile);
        return excelFile.delete();
    }

    @PutMapping("changeStudentClass")
    @ResponseBody
    public Student changeStudentClass(Long classId,String username){
        return  studentService.changeStudentClassByStudntId(classId,username);
    }

    @PostMapping("/teacher")
    @ResponseBody
    public Teacher saveTeacher(Teacher teacher){
        return teacherService.saveTeacher(teacher);
    }

    @PostMapping("/student")
    @ResponseBody
    public Student saveStudent(Student student){
        return studentService.saveStudent(student);
    }

    @DeleteMapping("/teacher/{username}")
    @ResponseBody
    public String deleteTeacher(@PathVariable String username){
        teacherService.deleteTeacher(username);
        return "success";
    }

    @DeleteMapping("/student/{username}")
    @ResponseBody
    public String deleteStudent(@PathVariable String username){
        studentService.deleteStudent(username);
        return "success";
    }


    @PostMapping
    @ResponseBody
    public Admin modify(Admin admin, MultipartFile file){
        admin.setUsername(SecurityResourceUtil.getUserDetailsUsername());
        admin.setIconPath(FileUploadUtil.uploadFileToFolder(file, prefixPath, "icon"));
        return  adminService.updateAdmin(admin);
    }

    @GetMapping("/students")
    @ResponseBody
    public List<Student> getStudents(){
        return studentService.findAll();
    }

    @GetMapping("/teachers")
    @ResponseBody
    public List<Teacher> getTeachers(){
        return teacherService.findAll();
    }


    @GetMapping("/classrooms")
    @ResponseBody
    public List<Classroom> getClassrooms(){
        return classroomService.findAll();
    }


    @PutMapping("/teacher")
    @ResponseBody
    public Teacher updateTeacher(Teacher teacher){
        return teacherService.updateTeacher(teacher);
    }

    @PutMapping("/student")
    @ResponseBody
    public Student updateStudent(Student student){
        return studentService.updateStudent(student);
    }

    @PostMapping("/teacher/classrooms")
    @ResponseBody
    public List<Classroom> updateTeacherClassrooms(@RequestParam String username, @RequestParam(required = false) Long[] classIds){
        return teacherService.updateTeacherClassrooms(username, classIds);
    }

    @DeleteMapping("/classroom/{classId}")
    @ResponseBody
    public String deleteClassroom(@PathVariable long classId){
        classroomService.deleteClassroomById(classId);
        return "success";
    }

}
